package com.localjoycommunity.cpdj.project.system.controller;

import com.localjoycommunity.cpdj.common.R;
import com.localjoycommunity.cpdj.common.interceptor.help.NeedLogin;
import com.localjoycommunity.cpdj.common.utils.poi.ExcelUtil;
import com.localjoycommunity.cpdj.framework.aspectj.lang.annotation.Log;
import com.localjoycommunity.cpdj.framework.aspectj.lang.enums.BusinessType;
import com.localjoycommunity.cpdj.framework.web.controller.BaseController;
import com.localjoycommunity.cpdj.framework.web.domain.AjaxResult;
import com.localjoycommunity.cpdj.framework.web.page.TableDataInfo;
import com.localjoycommunity.cpdj.project.system.domain.SysNews;
import com.localjoycommunity.cpdj.project.system.domain.UserNews;
import com.localjoycommunity.cpdj.project.system.service.ISysNewsService;
import com.localjoycommunity.cpdj.project.system.service.IUserNewsService;
import com.localjoycommunity.cpdj.project.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 新闻资讯Controller
 *
 * @author aqie
 * @date 2020-11-02
 */
@RestController
@RequestMapping("/system/news")
public class SysNewsController extends BaseController {
    @Autowired
    private ISysNewsService sysNewsService;

    @Autowired
    private IUserNewsService userNewsService;

    @Autowired
    private IUserService userService;

    /**
     * 用户给 新闻点赞
     */
    @NeedLogin
    @PostMapping("/digNews")
    public AjaxResult digNews(@RequestBody UserNews userNews, HttpServletRequest req) {
        Integer userId = getUserId(req);
        userNews.setUserId(userId);
        int i = userNewsService.newsDig(userNews);
        return AjaxResult.success(i);
    }

    /**
     * 查询新闻资讯列表
     */
    @GetMapping("/list/pc")
    public TableDataInfo listPC(SysNews sysNews) {
        startPage();
        List<SysNews> list = sysNewsService.selectSysNewsList2(sysNews);
        return getDataTable(list);
    }

    /**
     * 查询新闻资讯列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysNews sysNews) {
        startPage();
        List<SysNews> list = sysNewsService.selectSysNewsList1(sysNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻资讯列表
     */
    @Log(title = "新闻资讯", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysNews sysNews) {
        List<SysNews> list = sysNewsService.selectSysNewsList(sysNews);
        ExcelUtil<SysNews> util = new ExcelUtil<SysNews>(SysNews.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取新闻资讯详细信息
     */
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable("newsId") Long newsId,HttpServletRequest req) {
        SysNews sysNews = sysNewsService.selectSysNewsById(newsId);
        Integer userId = getUserId(req);
        if (userId != 0){
            Integer dig = userNewsService.isDig(userId, newsId);
            sysNews.setIsDig(dig);
        }
        return AjaxResult.success(sysNews);
    }

    /**
     * 获取新闻资讯详细信息app
     */
    @GetMapping(value = "/app/{newsId}")
    public R<SysNews> getInfoApp(@PathVariable("newsId") Long newsId,HttpServletRequest req) {
        SysNews sysNews = sysNewsService.selectSysNewsByIdApp(newsId);
        if (sysNews == null){
            return R.error("该新闻不存在");
        }
        Integer userId = getUserId(req);
        if (userId != 0){
            Integer dig = userNewsService.isDig(userId, newsId);
            sysNews.setIsDig(dig);
        }
        return R.success(sysNews);
    }

    /**
     * 新增新闻资讯
     */
    @Log(title = "新闻资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNews sysNews) {
        return toAjax(sysNewsService.insertSysNews(sysNews));
    }

    /**
     * 修改新闻资讯
     */
    @Log(title = "新闻资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNews sysNews) {
        return toAjax(sysNewsService.updateSysNews(sysNews));
    }

    /**
     * 删除新闻资讯
     */
    @Log(title = "新闻资讯", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds) {
        return toAjax(sysNewsService.deleteSysNewsByIds(newsIds));
    }
}
